package com.wanxi.app.service.user;

import com.wanxi.app.domain.dto.AppOrderDto;
import com.wanxi.app.domain.result.AppResult;
import com.wanxi.domain.code.ResultCode;
import com.wanxi.domain.vo.*;
import com.wanxi.orderApi.OrderFeign;
import com.wanxi.user.api.AddressFeign;
import com.wanxi.user.api.ShoppingCartFeign;
import com.wanxi.user.domain.entity.Address;
import com.wanxi.user.domain.entity.ShoppingCart;
import com.wanxi.user.domain.result.UserResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author:wgy
 * @Data：2022/1/17 14:02
 */
@Service
public class MyOrderService {

    @Resource
    private OrderFeign orderFeign;

    @Resource
    private AddressFeign addressFeign;

    @Resource
    private ShoppingCartFeign shoppingCartFeign;

    /**
     * 用户查看自己的订单
     * @param vo
     * @return
     */
    public AppResult<List<OrderVO>> findOrderByUserId(OrderQueryVO vo){
        List<OrderVO> customerOrdersById = orderFeign.getCustomerOrdersById(vo.getId(),new Paging());
        return AppResult.success(customerOrdersById);
    }

    /**
     * 用户确定收获
     * @param orderId
     * @return
     */
    public AppResult<ResultCode> receive(Long orderId){
        ResultCode receive = orderFeign.receive(orderId);
        return AppResult.success(receive);
    }
    /**
     * 用户逻辑删除自己的订单
     * @param orderId
     * @return
     */
    public AppResult<ResultCode> deleteOrderById(Long orderId){
        ResultCode receive = orderFeign.deleteOrderById(orderId);
        return AppResult.success(receive);
    }

    /**
     * 用户下单
     * @param appOrderDto
     * @return
     */
    public AppResult<String> addOrder(AppOrderDto appOrderDto){
//        获取购物车商品
        UserResult<List<ShoppingCart>> cartProduct = shoppingCartFeign.findCartProduct(appOrderDto.getUserId());
        List<ShoppingCart> ShoppingCartList = cartProduct.getData();

//        获取选择的收货地址
        Address address = addressFeign.findAddressById(appOrderDto.getAddressId()).getData();
//        拼接收货地址
        String addressDetail = address.getProvince() + address.getCity() + address.getDistrict() + address.getDetailAddress();
//        要添加到订单的商品列表
        List<OrderProductVO> orderProductVOList = new ArrayList<>();
        for (ShoppingCart shoppingCart: ShoppingCartList
             ) {
            OrderProductVO orderProductVO = new OrderProductVO();
            orderProductVO.setProductNum(shoppingCart.getProductNum());
            orderProductVO.setShopId(shoppingCart.getShopId());
            orderProductVO.setSkuId(shoppingCart.getSkuId());
            orderProductVOList.add(orderProductVO);
        }
//        添加订单信息
        String phone = address.getPhone();
        PlaceOrderVO placeOrderVO = new PlaceOrderVO();

        placeOrderVO.setAddress(addressDetail);
        placeOrderVO.setPhone(phone);
//        商铺id
        placeOrderVO.setProductVOS(orderProductVOList);
        placeOrderVO.setUserId(appOrderDto.getUserId());
        placeOrderVO.setCouponId(appOrderDto.getCouponId());
//        计算价格
        Map<String, Object> stringObjectMap = orderFeign.beforeSubmitOrder(placeOrderVO);
        String originAmount1 = (String) stringObjectMap.get("OriginAmount");

        BigDecimal bigDecimal = new BigDecimal(originAmount1);
//        添加订单
        ResultCode resultCode = orderFeign.addOrder(placeOrderVO);
        if (resultCode.getCode()==200){
            return AppResult.success(resultCode.getMessage());
        }
        else {
            return AppResult.failed();
        }
    }
}
